package com.management.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.management.demo.domain.management.*;
import com.management.demo.mapper.management.UserMapper;
import com.management.demo.service.*;
import com.management.demo.vo.management.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2025-04-01 15:38:17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserRoleRelationService userRoleRelationService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private MenuRoleRelationService menuRoleRelationService;

    @Override
    public List<RoleVO> getRoleListByUserId(Integer userId) {
        List<RoleVO> roles = new ArrayList<>();
        List<UserRoleRelation> userRoleRelations = userRoleRelationService.list(new LambdaQueryWrapper<UserRoleRelation>()
                .eq(UserRoleRelation::getUserId, userId));
        if (CollectionUtil.isNotEmpty(userRoleRelations)) {
            List<Integer> roleIdList = userRoleRelations.stream().map(UserRoleRelation::getRoleId).toList();
            List<Role> roleList = roleService.list(new LambdaQueryWrapper<Role>().in(Role::getId, roleIdList));
            roles = BeanUtil.copyToList(roleList, RoleVO.class);
        }
        return roles;
    }

    @Override
    public List<Menu> getACLListByUserId(Integer userId) {
        List<Menu> menuList = new ArrayList<>();
        // 设置 buttons routes
        List<UserRoleRelation> userRoleRelations = userRoleRelationService.list(new LambdaQueryWrapper<UserRoleRelation>().eq(UserRoleRelation::getUserId, userId));
        // 获取角色id
        List<Integer> roleIds = userRoleRelations.stream().map(UserRoleRelation::getRoleId).toList();
        if (CollectionUtil.isNotEmpty(roleIds)) {
            // 菜单和角色对应关系
            List<MenuRoleRelation> menuRoleRelations = menuRoleRelationService.list(new LambdaQueryWrapper<MenuRoleRelation>().in(MenuRoleRelation::getRoleId, roleIds));
            // 菜单列表
            List<Integer> menuIds = menuRoleRelations.stream().map(MenuRoleRelation::getMenuId).toList();
            if (CollectionUtil.isNotEmpty(menuIds)) {
                menuList = menuService.list(new LambdaQueryWrapper<Menu>().in(Menu::getId, menuIds));
            }
        }
        return menuList;
    }

    @Override
    public List<String> getRouteList(List<Menu> menuList) {
        return menuList.stream().filter(item -> item.getLevel().equals(1) || item.getLevel().equals(2)).map(Menu::getAcl).toList();
    }

    @Override
    public List<String> getButtonList(List<Menu> menuList) {
        return menuList.stream().filter(item -> item.getLevel().equals(3)).map(Menu::getAcl).toList();
    }
}




